// src/stores/crawlerTask.js
import { defineStore } from 'pinia'
import { crawlerTaskApi } from '@/api/crawlerTask'

export const useCrawlerTaskStore = defineStore('crawlerTask', {
    state: () => ({
        tasks: [],
        loading: false,
        currentTaskLogs: []
    }),

    actions: {
        async fetchTasks() {
            this.loading = true
            try {
                const response = await crawlerTaskApi.getTasks()
                this.tasks = response.data || []
            } finally {
                this.loading = false
            }
        },

        async createTask(task) {
            const response = await crawlerTaskApi.createTask(task)
            await this.fetchTasks()
            return response
        },

        async updateTask(id, task) {
            const response = await crawlerTaskApi.updateTask(id, task)
            await this.fetchTasks()
            return response
        },

        async deleteTask(id) {
            const response = await crawlerTaskApi.deleteTask(id)
            await this.fetchTasks()
            return response
        },

        async pauseTask(id) {
            const response = await crawlerTaskApi.pauseTask(id)
            await this.fetchTasks()
            return response
        },

        async resumeTask(id) {
            const response = await crawlerTaskApi.resumeTask(id)
            await this.fetchTasks()
            return response
        },

        async executeTask(id) {
            const response = await crawlerTaskApi.executeTask(id)
            await this.fetchTasks()
            return response
        },

        async fetchTaskLogs(id, limit = 50) {
            const response = await crawlerTaskApi.getTaskLogs(id, limit)
            this.currentTaskLogs = response.data || []
            return response
        }
    }
})